<template>
	<div class="addSubject">
		<el-button type="primary" size="mini" @click="addSubjectShow = true">
			<slot>新增主体3</slot>
		</el-button>

		<el-dialog width="30%" title="查询主体" :close-on-click-modal="false" :visible.sync="searchSubjectShow"
			append-to-body>
			<el-card>
				<el-input type="text" @change.native="filterSubjectForm" v-model="keywords"></el-input>

				<template v-for="(item, index) in SubjectForm">
					<el-card v-if="item.公民身份号码" shadow="never" class="result" @click.native="insertion(item)"
						:key="index">
						<h4><span>姓名:</span>{{ item.姓名 }}-({{ item.性别 }})</h4>
						<p><span>公民身份号码:</span>{{ item.公民身份号码 }}</p>
						<p><span>民族:</span>{{ item.民族 }}</p>
						<p><span>文化程度:</span>{{ item.文化程度 }}</p>
						<p><span>职业:</span>{{ item.职业 }}</p>
						<p><span>居村委名称:</span>{{ item.居村委名称 }}</p>
						<p><span>地址:</span>{{ item.地址 }}</p>
						<p><span>文化程度:</span>{{ item.文化程度 }}</p>
					</el-card>
					<el-card v-else shadow="never" class="result" @click.native="insertion(item)" :key="index">
						<h4><span>注册号:</span>{{ item.注册号 }}</h4>
						<p><span>企业名称:</span>{{ item.企业名称 }}</p>
						<p><span>地址:</span>{{ item.地址 }}</p>
						<p><span>经营范围:</span>{{ item.经营范围 }}</p>
						<p><span>成立日期:</span>{{ item.成立日期 }}</p>
						<p><span>注册资本_万元:</span>{{ item.注册资本_万元 }}</p>
						<p><span>企业类型:</span>{{ item.企业类型 }}</p>
						<p><span>企业经济性质:</span>{{ item.企业经济性质 }}</p>
						<p><span>管辖单位:</span>{{ item.管辖单位 }}</p>
						<p><span>登记机关:</span>{{ item.登记机关 }}</p>
						<p><span>企业经济性质:</span>{{ item.企业经济性质 }}</p>
						<p><span>联系电话:</span>{{ item.联系电话 }}</p>
						<p><span>企业状态:</span>{{ item.企业状态 }}</p>
						<p><span>企业经济性质:</span>{{ item.企业经济性质 }}</p>
						<p><span>经营者身份证号码:</span>{{ item.经营者身份证号码 }}</p>
						<p><span>负责人:</span>{{ item.负责人 }}</p>
					</el-card>
				</template>
				<div class="dialog-footer" style="float:right;padding:15px 0">
					<el-button type="primary" @click="searchSubjectShow = false">关闭查询窗口</el-button>
				</div>
			</el-card>
		</el-dialog>

		<el-dialog title="经营主体" :append-to-body="true" label-width="100px" width="80%" :visible.sync="addSubjectShow">
			<!--      <el-form :model="form">-->
			<!--		  &lt;!&ndash; autocomplete="off"-->
			<!--		  @click.native="searchSubject" &ndash;&gt;-->
			<!--        <el-form-item label="经营主体">-->
			<!--          <el-input-->
			<!--            v-model="form.subjectName" ></el-input>-->
			<!--        </el-form-item>-->
			<!--        <el-form-item label="占用面积(亩)">-->
			<!--          <el-input v-model="form.lsSubjectUseArea" autocomplete="off"></el-input>-->
			<!--        </el-form-item>-->
			<!--        <el-form-item label="年产值(万元)">-->
			<!--          <el-input v-model="form.lsYearValue" autocomplete="off"></el-input>-->
			<!--        </el-form-item>-->
			<!--        <el-form-item label="所属基地">-->
			<!--          <el-input v-model="form.subjectBase" autocomplete="off"></el-input>-->
			<!--        </el-form-item>-->
			<!--      </el-form>-->
            <el-button type="primary" style="margin-bottom: 20px;display:none"   @click="openAdd()" >新增主体</el-button>
            <el-table 
			ref="multipleTable" 
			:data="SubjectInfoList"
			style="width: 100%"
			@select='selection'
			@select-all='selectionAll'
			>
                <!-- <el-table-column type="selection" prop="selected" width="55">
                </el-table-column> -->
                <el-table-column prop="id"  type="selection" width="50">
                </el-table-column>
                <el-table-column prop="subjectName" label="主体名称" width="130">
                </el-table-column>
                <el-table-column prop="subjectLinkman" label="联系人" width="160">
                </el-table-column>
                <el-table-column prop="subjectType" label="主体类别" width="160">
                </el-table-column>
                <el-table-column prop="subjectAddr" label="主体地址" width="320">
                </el-table-column>

                <el-table-column prop="subjectOrgCode" label="主体组织机构代码" width="230">
                </el-table-column>

                <el-table-column prop="subjectTele" label="电话" width="160">
                </el-table-column>

                <el-table-column prop="subjectBase" label="基地" width="160">
                </el-table-column>
<!--                <el-table-column label="操作" fixed="right" width="120">-->
<!--                    <template slot-scope="scope">-->
<!--                        <el-button @click="handleClick(scope.row,'look')" type="text" size="small">查看-->
<!--                        </el-button>-->
<!--                        <el-button type="text" size="small" @click="handleClick(scope.row,'edit')">编辑-->
<!--                        </el-button>-->
<!--                    </template>-->
<!--                </el-table-column>-->
            </el-table>
            <div class="bottom">
                <el-pagination background @size-change="handleSizeChange" @current-change="currentchange"
                               :current-page="paging.pageNum" :page-sizes="[5, 10, 20, 30, 40]"
                               :page-size="paging.pageSize" layout="total, sizes, prev, pager, next, jumper"
                               :total="paging.total">
                </el-pagination>
            </div>
			<div slot="footer" class="dialog-footer">
				<el-button @click="addSubjectShow = false">取 消</el-button>
				<el-button type="primary" @click="submit()"  style="display:none">确 定</el-button>
			</div>
		</el-dialog>

        <el-dialog title="新增生产主体" :append-to-body="true" :visible.sync="AddDialogFormVisible" :close-on-click-modal="false"
                   :close-on-press-escape="false">
            <el-card>
                <el-tabs v-model="typeClass" stretch>
                    <el-tab-pane label="个体户" name="个体户">
                        <Individual @on-close="closeAddModel()" @success="addIndividual"></Individual>
                    </el-tab-pane>
                    <el-tab-pane label="合作社" name="合作社">
                        <Cooperation @on-close="closeAddModel()" @success="addIndividual"></Cooperation>
                    </el-tab-pane>
                    <el-tab-pane label="企业" name="企业">
                        <Enterprise @on-close="closeAddModel()" @success="addIndividual"></Enterprise>
                    </el-tab-pane>
                </el-tabs>
            </el-card>
            <!--			<el-button @click="AddDialogFormVisible = false" type="default" class="btn-block" style="margin-top: 15px">-->
            <!--				关闭窗口</el-button>-->
        </el-dialog>
	</div>
</template>

<script>
    import Individual from "@/views/DataResource/ProductionBase/Individual/Individual.vue";
    import Cooperation from "@/views/DataResource/ProductionBase/Cooperation/Cooperation";
    import Enterprise from "@/views/DataResource/ProductionBase/Enterprise/Enterprise";
	export default {
		name: "",
		props: {
			landCodeT: {
				type: [String,Array],
				required:true
			},
			addType:{
				type:Number,
				default:0
			}
			// tableData: {
			// 	type: Array
			// }
		},
        components: {
            Individual,
            Cooperation,
            Enterprise
        },
		data() {
			return {
                typeClass: '个体户',
				keywords: "",
				formLabelWidth: "100px",
				addSubjectShow: false,
				searchSubjectShow: false,
				form: {
					subjectName: "",
					base: "默认基地",
				},
                params: {
                    pageNumber: 1,
                    pageSize: 10,
                },
                paging: {},
				SearchFrom: [],
				SubjectForm: [],
				tableData: [],
                List: [],
                SubjectInfoList: [],
                multipleSelection: [],
                AddDialogFormVisible: false,
			};
		},
		created() {
            this.getSubjectInfoList();
		},
		computed: {
		},
		methods: {
            openAdd () {
                this.AddDialogFormVisible = true;
            },
            addIndividual(res) {
                // console.log(res);
                this.AddDialogFormVisible = res.visible;
                if (res.code === 200) {
                    this.getSubjectInfoList();
                }
            },
            closeAddModel() {
                this.AddDialogFormVisible = false
            },
			selection(selection, row){
               this.multipleSelection = selection
			},
			selectionAll(selection){
                this.multipleSelection = selection
			},
            handleSizeChange(pageSize) {
                this.params.pageSize = pageSize;
                this.getSubjectInfoList(this.params.pageNumber, this.params.pageSize);
            },
            currentchange(pageNumber) {
                this.params.pageNumber = pageNumber;
                this.getSubjectInfoList(this.params.pageNumber, this.params.pageSize);
            },
            getSubjectInfoList(pageNumber = 1, pageSize = 10) {
                this.$fishing.getSubjectInfoList(pageNumber, pageSize).then((res) => {
                    this.SubjectInfoList = res.data.list.filter((item) => {
                        if (item.subjectType == 'P0201') {
                            item.subjectType = '个体'
                        } else if (item.subjectType == 'P0202') {
                            item.subjectType = '合作社'
                        } else if (item.subjectType == 'P0203') {
                            item.subjectType = '企业'
                        }
                        return item
                    })
                    delete res.data.list;
                    this.paging = res.data;
                });
            },
			filterSubjectForm() {
				this.SubjectForm = [];
				var keyword = this.keywords;
				if (keyword) {
					fetch("/ku.json")
						.then((res) => res.json())
						.then((res) => {
							res.forEach((item) => {
								if (item.姓名.indexOf(keyword) != -1) {
									this.SubjectForm.push(item);
								}
							});
						});

					// fetch('/qy.json')
					//     .then(res => res.json())
					//     .then(res => {
					//         res.forEach((item) => {
					//             this.SubjectForm.push(item)
					//         })
					//     })
				} else {
					this.SubjectForm = [];
				}
			},
			insertion(item) {
				this.form.subjectName = item.姓名 || item.企业名称;
				this.from = item
				this.searchSubjectShow = false;
			},
			searchSubject() {
				this.searchSubjectShow = true;
			},
            toggleSelection(rows) {
                if (rows) {
                    rows.forEach(row => {
                        this.$refs.multipleTable.toggleRowSelection(row);
                    });
                } else {
                    this.$refs.multipleTable.clearSelection();
                }
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
			submit() {
                if(this.addType===1){
                    this.resend();
				}else{
					this.addSubject()
				}
			},
            resend () {
	            this.$emit('on-click',this.multipleSelection)
				this.addSubjectShow = false;
            },
			addSubject(){
				let result = this.multipleSelection
                for (let i = 0; i < result.length; i++) {
                    let params = {
                        lsPlanId: '',
                        lsLandCode: this.landCodeT,
                        lsSubjectCode: result[i].subjectCode,
                        lsSubjectUseArea: '',
                        lsYearValue: ''
                    }
                    this.$axios.post('/rbac/data/LandSubject/add', {
                        params: params
                    }).then((res) => {
                        console.log(res)
                        if (res.result !== "fail") {
                            this.$message.success('操作完成');
                            this.addSubjectShow = false;
                            this.$emit('on-click', result)
                        } else {
                            this.$message.error(res.errorMsg);
                            this.addSubjectShow = false;
                        }
                    })
                }

			}
		},
	};
</script>

<style lang="scss" scoped>
	.result {
		margin: 10px 0;
		border: 1px solid #ddd;
		border-radius: 5px;

		h4 {
			padding-bottom: 15px;
			border-bottom: 1px solid #ddd;
			margin-bottom: 15px;
		}

		p {
			line-height: 25px;
		}
	}
</style>
